package com.cqjtu.email;

import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/12 0012.
 */
public class POP3Main {
    public static int count;
    private static String sender;
    private static String subject;
    private static String date;
    public static boolean over = false;
    public static List<MailContent> mailList = new ArrayList<MailContent>();


    public void look() {
        int POP3port = 110;
        //List<MailContent> mailList = new ArrayList<MailContent>();
        Socket client = null;
        try {
            //向pop3服务程序建立一个套接字连接
            client = new Socket("pop3.163.com", POP3port);
            //读取输出
            InputStream is = client.getInputStream();
            BufferedReader sockin = new BufferedReader(new InputStreamReader(is));
            //创建一个PrintWriter对象，以便向套接字写入内容
            OutputStream os = client.getOutputStream();
            PrintWriter sockout = new PrintWriter(os, true);
            //显示同SMTP服务程序的握手过程
            System.out.println("S:" + sockin.readLine());
            sockout.println("user " + MainActivity.username);
            System.out.println("S:" + sockin.readLine());
            sockout.println("pass " + MainActivity.password);
            System.out.println("S:" + sockin.readLine());
            sockout.println("stat");
            String temp[] = sockin.readLine().split(" ");
            count = Integer.parseInt(temp[1]);//信封中共有多少封邮箱
            int min = Math.min(count, 100);//接受100封邮箱
            for (int i = 1; i < count + 1; i++) {
                sockout.println("retr " + i);
                //System.out.println("以下为第" + i + "封邮件的内容");
                StringBuilder sb = new StringBuilder();
                while (true) {
                    String reply = sockin.readLine();
                    // System.out.println(reply);
                    if (reply.contains("From:")) {
                        sender = reply;
                    } else if (reply.contains("Subject:")) {
                        subject = reply;
                    } else if (reply.contains("Date:")) {
                        date = reply;
                    } else {
                        sb.append(reply);
                    }
                    if (reply.toLowerCase().equals(".")) {
                        break;
                    }
                }
                Log.i("email", "look: " + sb.toString());
                MailContent mailcontent = new MailContent(sender, subject, date);
                mailcontent.setDetail(sb.toString());

                mailList.add(mailcontent);
                Log.i("MAIL_SIZE", "look: " + mailList.size());
            }
            over = true;
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (client != null) {
                    client.close();
                }
            } catch (IOException e) {
            }
        }
    }
}
